package com.inei.evaluacionmype.controller.activity;

import java.util.ArrayList;
import java.util.List;
import com.inei.evaluacionmype.R;
import com.inei.evaluacionmype.controller.CapituloIIController;
import com.inei.evaluacionmype.controller.JustificacionController;
import com.inei.evaluacionmype.model.entity.Cap02;
import com.inei.evaluacionmype.model.entity.Justificacion;
import com.inei.evaluacionmype.tools.Mensaje;
import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TableRow;

public class CapituloII_Activity extends Fragment {

	// componentes
	private View root;
	private TableRow trC2011, trC203, trC204, trC2061, trC2062, trC207, trC208,
			trC209, trC210, trC213;
	private EditText etC2_01, etC2_02, etC2_03, etC2_04, etC2_05, etC2_06,
			etC2_07, etC2_08, etC2_09, etC2_10, etC2_11, etC2_12, etC2_13,
			etC2011, etC203, etC204, etC2061, etC2062, etC207, etC208, etC209,
			etC210, etC213;

	// variables
	private CapituloIIController con;
	private Cap02 obj;
	private JustificacionController con2;
	private List<Justificacion> lJust;
	private String ruc_id;
	private boolean nuevo = false;
	private boolean nuevoJust = false;
	private Mensaje mensaje;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		root = inflater
				.inflate(R.layout.activity_capitulo_ii, container, false);

		return root;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		con = new CapituloIIController(getActivity());
		con2 = new JustificacionController(getActivity());
		mensaje = new Mensaje(getActivity());

		IniciarComponentes();

		CargarDatos();

		AgregarEventos();
	}

	private void IniciarComponentes() {
		trC2011 = (TableRow) root.findViewById(R.id.trC2011);
		trC203 = (TableRow) root.findViewById(R.id.trC203);
		trC204 = (TableRow) root.findViewById(R.id.trC204);
		trC2061 = (TableRow) root.findViewById(R.id.trC2061);
		trC2062 = (TableRow) root.findViewById(R.id.trC2062);
		trC207 = (TableRow) root.findViewById(R.id.trC207);
		trC208 = (TableRow) root.findViewById(R.id.trC208);
		trC209 = (TableRow) root.findViewById(R.id.trC209);
		trC210 = (TableRow) root.findViewById(R.id.trC210);
		trC213 = (TableRow) root.findViewById(R.id.trC213);

		Ocultar();

		etC2_01 = (EditText) root.findViewById(R.id.etC2_01);
		etC2_02 = (EditText) root.findViewById(R.id.etC2_02);
		etC2_03 = (EditText) root.findViewById(R.id.etC2_03);
		etC2_04 = (EditText) root.findViewById(R.id.etC2_04);
		etC2_05 = (EditText) root.findViewById(R.id.etC2_05);
		etC2_06 = (EditText) root.findViewById(R.id.etC2_06);
		etC2_07 = (EditText) root.findViewById(R.id.etC2_07);
		etC2_08 = (EditText) root.findViewById(R.id.etC2_08);
		etC2_09 = (EditText) root.findViewById(R.id.etC2_09);
		etC2_10 = (EditText) root.findViewById(R.id.etC2_10);
		etC2_11 = (EditText) root.findViewById(R.id.etC2_11);
		etC2_12 = (EditText) root.findViewById(R.id.etC2_12);
		etC2_13 = (EditText) root.findViewById(R.id.etC2_13);
		etC2011 = (EditText) root.findViewById(R.id.etC2011);
		etC203 = (EditText) root.findViewById(R.id.etC203);
		etC204 = (EditText) root.findViewById(R.id.etC204);
		etC2061 = (EditText) root.findViewById(R.id.etC2061);
		etC2062 = (EditText) root.findViewById(R.id.etC2062);
		etC207 = (EditText) root.findViewById(R.id.etC207);
		etC208 = (EditText) root.findViewById(R.id.etC208);
		etC209 = (EditText) root.findViewById(R.id.etC209);
		etC210 = (EditText) root.findViewById(R.id.etC210);
		etC213 = (EditText) root.findViewById(R.id.etC213);
	}

	private void Ocultar() {
		trC2011.setVisibility(View.GONE);
		trC203.setVisibility(View.GONE);
		trC204.setVisibility(View.GONE);
		trC2061.setVisibility(View.GONE);
		trC2062.setVisibility(View.GONE);
		trC207.setVisibility(View.GONE);
		trC208.setVisibility(View.GONE);
		trC209.setVisibility(View.GONE);
		trC210.setVisibility(View.GONE);
		trC213.setVisibility(View.GONE);
	}

	private void AgregarEventos() {
		EventoClave06(etC2_01);
		EventoClave06(etC2_02);
		EventoClave06(etC2_03);
		EventoClave06(etC2_04);
		EventoClave06(etC2_05);

		EventoClave13(etC2_07);
		EventoClave13(etC2_08);
		EventoClave13(etC2_09);
		EventoClave13(etC2_10);
		EventoClave13(etC2_11);
		EventoClave13(etC2_12);
	}

	private void EventoClave06(EditText et) {
		et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				int n01 = Integer.parseInt(etC2_01.getText().toString().trim()
						.length() == 0 ? "0" : etC2_01.getText().toString());
				int n02 = Integer.parseInt(etC2_02.getText().toString().trim()
						.length() == 0 ? "0" : etC2_02.getText().toString());
				int n03 = Integer.parseInt(etC2_03.getText().toString().trim()
						.length() == 0 ? "0" : etC2_03.getText().toString());
				int n04 = Integer.parseInt(etC2_04.getText().toString().trim()
						.length() == 0 ? "0" : etC2_04.getText().toString());
				int n05 = Integer.parseInt(etC2_05.getText().toString().trim()
						.length() == 0 ? "0" : etC2_05.getText().toString());
				int sum06 = n01 + n02 + n03 + n04 + n05;

				etC2_06.setText((sum06) + "");
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	private void EventoClave13(EditText et) {
		et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				int n07 = Integer.parseInt(etC2_07.getText().toString().trim()
						.length() == 0 ? "0" : etC2_07.getText().toString());
				int n08 = Integer.parseInt(etC2_08.getText().toString().trim()
						.length() == 0 ? "0" : etC2_08.getText().toString());
				int n09 = Integer.parseInt(etC2_09.getText().toString().trim()
						.length() == 0 ? "0" : etC2_09.getText().toString());
				int n10 = Integer.parseInt(etC2_10.getText().toString().trim()
						.length() == 0 ? "0" : etC2_10.getText().toString());
				int n11 = Integer.parseInt(etC2_11.getText().toString().trim()
						.length() == 0 ? "0" : etC2_11.getText().toString());
				int n12 = Integer.parseInt(etC2_12.getText().toString().trim()
						.length() == 0 ? "0" : etC2_12.getText().toString());

				etC2_13.setText((n07 + n08 + n09 + n10 + n11 + n12) + "");
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	private void CargarDatos() {
		ruc_id = getArguments().getString("ruc_id");
		obj = new Cap02();
		obj.setRUC(ruc_id);

		obj = con.Cap02Obtener(obj);
		if (obj != null) {
			etC2_01.setText(obj.getC2P1_701() + "");
			etC2_02.setText(obj.getC2P2_702() + "");
			etC2_03.setText(obj.getC2P3_703() + "");
			etC2_04.setText(obj.getC2P4_704() + "");
			etC2_05.setText(obj.getC2P5_75() + "");
			etC2_06.setText(obj.getC2P6_TOTAL() + "");
			etC2_07.setText(obj.getC2P7_601() + "");
			etC2_08.setText(obj.getC2P8_602() + "");
			etC2_09.setText(obj.getC2P9_62() + "");
			etC2_10.setText(obj.getC2P10_63() + "");
			etC2_11.setText(obj.getC2P11_64() + "");
			etC2_12.setText(obj.getC2P12_65() + "");
			etC2_13.setText(obj.getC2P13_TOTAL() + "");
		}

		ValidarCap();

		lJust = con2.JustificacionObtenerPorRuc(ruc_id, "2");
		if (lJust == null) {
			nuevoJust = true;
			lJust = new ArrayList<Justificacion>();
			for (int i = 0; i < 10; i++) {
				Justificacion just = new Justificacion();
				just.setRUC(ruc_id);
				just.setCAPITULO("2");
				just.setCLAVE(i + "");
				just.setFLAG("1");
				lJust.add(i, just);
			}
			lJust.get(0).setREGLA("MP_02O01: Cap 02, col 01, clave 06 > 0");
			lJust.get(1).setREGLA("MP_02O02: Cap 02, col 01, clave 13 > 0");
			lJust.get(2).setREGLA("MP_02O03: Cap 02, col 01, clave 09 > 0");
			lJust.get(3).setREGLA("MP_02O04: Cap 02, col 01, clave 10 > 0");
			lJust.get(4)
					.setREGLA(
							"MP_02R01: Si Cap 02, col 01, clave 01 > 0 entonces Cap 02, col 01, clave 07 > 0");
			lJust.get(5)
					.setREGLA(
							"MP_02R01_I: Si Cap 02, col 01, clave 07 > 0 entonces Cap 02, col 01, clave 01 > 0");
			lJust.get(6)
					.setREGLA(
							"MP_02R02: Si Cap 02, col 01, claves 02+03 > 0 entonces Cap 02, col 01, clave 08 > 0");
			lJust.get(7)
					.setREGLA(
							"MP_02R02_I: Si Cap 02, col 01, clave 08 > 0 entonces Cap 02, col 01, claves 02+03 > 0");
			lJust.get(8)
					.setREGLA(
							"MP_02P01: Cap 02, col 01, clave 06 > Cap 02, col 01, clave 13");
			lJust.get(9)
					.setREGLA(
							"MP_02P02: Cap 02, col 01, claves 02+03+04 > Cap 02, col 01, clave 01");
		} else {
			if (lJust.get(0).getDETALLE() != null) {
				etC2061.setText(lJust.get(0).getDETALLE());
			}
			if (lJust.get(1).getDETALLE() != null) {
				etC213.setText(lJust.get(1).getDETALLE());
			}
			if (lJust.get(2).getDETALLE() != null) {
				etC209.setText(lJust.get(2).getDETALLE());
			}
			if (lJust.get(3).getDETALLE() != null) {
				etC210.setText(lJust.get(3).getDETALLE());
			}
			if (lJust.get(4).getDETALLE() != null) {
				etC207.setText(lJust.get(4).getDETALLE());
			}
			if (lJust.get(5).getDETALLE() != null) {
				etC2011.setText(lJust.get(5).getDETALLE());
			}
			if (lJust.get(6).getDETALLE() != null) {
				etC208.setText(lJust.get(6).getDETALLE());
			}
			if (lJust.get(7).getDETALLE() != null) {
				etC203.setText(lJust.get(7).getDETALLE());
			}
			if (lJust.get(8).getDETALLE() != null) {
				etC2062.setText(lJust.get(8).getDETALLE());
			}
			if (lJust.get(9).getDETALLE() != null) {
				etC204.setText(lJust.get(9).getDETALLE());
			}
		}
	}

	public void ValidarCap() {
		int c01 = Integer
				.parseInt(etC2_01.getText().toString().length() == 0 ? "0"
						: etC2_01.getText().toString());
		int c02 = Integer
				.parseInt(etC2_02.getText().toString().length() == 0 ? "0"
						: etC2_02.getText().toString());
		int c03 = Integer
				.parseInt(etC2_03.getText().toString().length() == 0 ? "0"
						: etC2_03.getText().toString());
		int c04 = Integer
				.parseInt(etC2_04.getText().toString().length() == 0 ? "0"
						: etC2_04.getText().toString());
		int c06 = Integer
				.parseInt(etC2_06.getText().toString().length() == 0 ? "0"
						: etC2_06.getText().toString());
		int c07 = Integer
				.parseInt(etC2_07.getText().toString().length() == 0 ? "0"
						: etC2_07.getText().toString());
		int c08 = Integer
				.parseInt(etC2_08.getText().toString().length() == 0 ? "0"
						: etC2_08.getText().toString());
		int c09 = Integer
				.parseInt(etC2_09.getText().toString().length() == 0 ? "0"
						: etC2_09.getText().toString());
		int c10 = Integer
				.parseInt(etC2_10.getText().toString().length() == 0 ? "0"
						: etC2_10.getText().toString());
		int c13 = Integer
				.parseInt(etC2_13.getText().toString().length() == 0 ? "0"
						: etC2_13.getText().toString());
		
		if (c07 > 0 && c01 == 0) {
			trC2011.setVisibility(View.VISIBLE);
		} else {
			etC2011.setText("");
			trC2011.setVisibility(View.GONE);
		}
		if (c08 > 0 && (c02 + c03) == 0) {
			trC203.setVisibility(View.VISIBLE);
		} else {
			etC203.setText("");
			trC203.setVisibility(View.GONE);
		}
		if ((c02 + c03 + c04) <= c01) {
			trC204.setVisibility(View.VISIBLE);
		} else {
			etC204.setText("");
			trC204.setVisibility(View.GONE);
		}
		if (c06 == 0) {
			trC2061.setVisibility(View.VISIBLE);
		} else {
			etC2061.setText("");
			trC2061.setVisibility(View.GONE);
		}
		if (c06 <= c13) {
			trC2062.setVisibility(View.VISIBLE);
		} else {
			etC2062.setText("");
			trC2062.setVisibility(View.GONE);
		}
		if (c01 > 0 && c07 == 0) {
			trC207.setVisibility(View.VISIBLE);
		} else {
			etC207.setText("");
			trC207.setVisibility(View.GONE);
		}
		if ((c02 + c03) > 0 && c08 == 0) {
			trC208.setVisibility(View.VISIBLE);
		} else {
			etC208.setText("");
			trC208.setVisibility(View.GONE);
		}
		if (c09 == 0) {
			trC209.setVisibility(View.VISIBLE);
		} else {
			etC209.setText("");
			trC209.setVisibility(View.GONE);
		}
		if (c10 == 0) {
			trC210.setVisibility(View.VISIBLE);
		} else {
			etC210.setText("");
			trC210.setVisibility(View.GONE);
		}
		if (c13 == 0) {
			trC213.setVisibility(View.VISIBLE);
		} else {
			etC213.setText("");
			trC213.setVisibility(View.GONE);
		}
	}

	public void GuardarCap() {
		if (obj == null) {
			nuevo = true;
			obj = new Cap02();
			obj.setRUC(ruc_id);
		}

		obj.setC2P1_701(Integer.parseInt(etC2_01.getText().toString().trim()
				.length() == 0 ? "0" : etC2_01.getText().toString()));
		obj.setC2P2_702(Integer.parseInt(etC2_02.getText().toString().trim()
				.length() == 0 ? "0" : etC2_02.getText().toString()));
		obj.setC2P3_703(Integer.parseInt(etC2_03.getText().toString().trim()
				.length() == 0 ? "0" : etC2_03.getText().toString()));
		obj.setC2P4_704(Integer.parseInt(etC2_04.getText().toString().trim()
				.length() == 0 ? "0" : etC2_04.getText().toString()));
		obj.setC2P5_75(Integer.parseInt(etC2_05.getText().toString().trim()
				.length() == 0 ? "0" : etC2_05.getText().toString()));
		obj.setC2P6_TOTAL(Integer.parseInt(etC2_06.getText().toString().trim()
				.length() == 0 ? "0" : etC2_06.getText().toString()));
		obj.setC2P7_601(Integer.parseInt(etC2_07.getText().toString().trim()
				.length() == 0 ? "0" : etC2_07.getText().toString()));
		obj.setC2P8_602(Integer.parseInt(etC2_08.getText().toString().trim()
				.length() == 0 ? "0" : etC2_08.getText().toString()));
		obj.setC2P9_62(Integer.parseInt(etC2_09.getText().toString().trim()
				.length() == 0 ? "0" : etC2_09.getText().toString()));
		obj.setC2P10_63(Integer.parseInt(etC2_10.getText().toString().trim()
				.length() == 0 ? "0" : etC2_10.getText().toString()));
		obj.setC2P11_64(Integer.parseInt(etC2_11.getText().toString().trim()
				.length() == 0 ? "0" : etC2_11.getText().toString()));
		obj.setC2P12_65(Integer.parseInt(etC2_12.getText().toString().trim()
				.length() == 0 ? "0" : etC2_12.getText().toString()));
		obj.setC2P13_TOTAL(Integer.parseInt(etC2_13.getText().toString().trim()
				.length() == 0 ? "0" : etC2_13.getText().toString()));

		if (nuevo) {
			if (con.Cap02Insertar(obj) > 0) {
				nuevo = false;
				ValidarJust();
				mensaje.Exito("SE GRABO CON EXITO");
			} else {
				mensaje.Error("ERROR AL GRABAR");
			}
		} else {
			if (con.Cap02Modificar(obj) > 0) {
				ValidarJust();
				mensaje.Exito("SE MODIFICO CON EXITO");
			} else {
				mensaje.Error("ERROR AL MODIFICAR");
			}
		}
	}

	private void ValidarJust() {
		int c01 = Integer
				.parseInt(etC2_01.getText().toString().length() == 0 ? "0"
						: etC2_01.getText().toString());
		int c02 = Integer
				.parseInt(etC2_02.getText().toString().length() == 0 ? "0"
						: etC2_02.getText().toString());
		int c03 = Integer
				.parseInt(etC2_03.getText().toString().length() == 0 ? "0"
						: etC2_03.getText().toString());
		int c04 = Integer
				.parseInt(etC2_04.getText().toString().length() == 0 ? "0"
						: etC2_04.getText().toString());
		int c06 = Integer
				.parseInt(etC2_06.getText().toString().length() == 0 ? "0"
						: etC2_06.getText().toString());
		int c07 = Integer
				.parseInt(etC2_07.getText().toString().length() == 0 ? "0"
						: etC2_07.getText().toString());
		int c08 = Integer
				.parseInt(etC2_08.getText().toString().length() == 0 ? "0"
						: etC2_08.getText().toString());
		int c09 = Integer
				.parseInt(etC2_09.getText().toString().length() == 0 ? "0"
						: etC2_09.getText().toString());
		int c10 = Integer
				.parseInt(etC2_10.getText().toString().length() == 0 ? "0"
						: etC2_10.getText().toString());
		int c13 = Integer
				.parseInt(etC2_13.getText().toString().length() == 0 ? "0"
						: etC2_13.getText().toString());

		if (c06 == 0 && etC2061.getText().toString().trim().length() == 0) {
			lJust.get(0).setDETALLE(etC2061.getText().toString());
			lJust.get(0).setFLAG("0");
		} else {
			lJust.get(0).setDETALLE(etC2061.getText().toString());
			lJust.get(0).setFLAG("1");
		}
		if (c13 == 0 && etC213.getText().toString().trim().length() == 0) {
			lJust.get(1).setDETALLE(etC213.getText().toString());
			lJust.get(1).setFLAG("0");
		} else {
			lJust.get(1).setDETALLE(etC213.getText().toString());
			lJust.get(1).setFLAG("1");
		}
		if (c09 == 0 && etC209.getText().toString().trim().length() == 0) {
			lJust.get(2).setDETALLE(etC209.getText().toString());
			lJust.get(2).setFLAG("0");
		} else {
			lJust.get(2).setDETALLE(etC209.getText().toString());
			lJust.get(2).setFLAG("1");
		}
		if (c10 == 0 && etC210.getText().toString().trim().length() == 0) {
			lJust.get(3).setDETALLE(etC210.getText().toString());
			lJust.get(3).setFLAG("0");
		} else {
			lJust.get(3).setDETALLE(etC210.getText().toString());
			lJust.get(3).setFLAG("1");
		}
		if ((c01 > 0 && c07 == 0)
				&& etC207.getText().toString().trim().length() == 0) {
			lJust.get(4).setDETALLE(etC207.getText().toString());
			lJust.get(4).setFLAG("0");
		} else {
			lJust.get(4).setDETALLE(etC207.getText().toString());
			lJust.get(4).setFLAG("1");
		}
		if ((c07 > 0 && c01 == 0)
				&& etC2011.getText().toString().trim().length() == 0) {
			lJust.get(5).setDETALLE(etC2011.getText().toString());
			lJust.get(5).setFLAG("0");
		} else {
			lJust.get(5).setDETALLE(etC2011.getText().toString());
			lJust.get(5).setFLAG("1");
		}
		if (((c02 + c03) > 0 && c08 == 0)
				&& etC208.getText().toString().trim().length() == 0) {
			lJust.get(6).setDETALLE(etC208.getText().toString());
			lJust.get(6).setFLAG("0");
		} else {
			lJust.get(6).setDETALLE(etC208.getText().toString());
			lJust.get(6).setFLAG("1");
		}
		if ((c08 > 0 && (c02 + c03) == 0)
				&& etC203.getText().toString().trim().length() == 0) {
			lJust.get(7).setDETALLE(etC203.getText().toString());
			lJust.get(7).setFLAG("0");
		} else {
			lJust.get(7).setDETALLE(etC203.getText().toString());
			lJust.get(7).setFLAG("1");
		}
		if ((c06 <= c13) && etC2062.getText().toString().trim().length() == 0) {
			lJust.get(8).setDETALLE(etC2062.getText().toString());
			lJust.get(8).setFLAG("0");
		} else {
			lJust.get(8).setDETALLE(etC2062.getText().toString());
			lJust.get(8).setFLAG("1");
		}
		if (((c02 + c03 + c04) <= c01)
				&& etC204.getText().toString().trim().length() == 0) {
			lJust.get(9).setDETALLE(etC204.getText().toString());
			lJust.get(9).setFLAG("0");
		} else {
			lJust.get(9).setDETALLE(etC204.getText().toString());
			lJust.get(9).setFLAG("1");
		}

		if (nuevoJust) {
			for (int i = 0; i < 10; i++) {
				con2.JustificacionInsertar(lJust.get(i));
			}
			nuevoJust = false;
		} else {
			for (int i = 0; i < 10; i++) {
				con2.JustificacionModificar(lJust.get(i));
			}
		}
	}
}
